package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 付诚
 * @Date: 2024/5/6 16:01
 * @Description:
 */
@Service
public class MarketAddressServiceImpl implements MarketAddressService {
    @Autowired
    private MarketAddressMapper marketAddressMapper;

    @Override
    public List<MarketAddress> list(Integer page, Integer limit, String name, String userId, String sort, String order) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();

        // todo 解决SQL注入风险
//        if (!ALLOWED_SORT_FIELDS.contains(sort) || !ALLOWED_SORT_ORDERS.contains(order.toLowerCase())) {
//            throw new IllegalArgumentException("无效的排序或顺序参数。");
//        }
        marketAddressExample.setOrderByClause(sort + " " + order);
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }

        //两个参数：当前页；每页数量
        PageHelper.startPage(page, limit);

        List<MarketAddress> marketAddressList = marketAddressMapper.selectByExampleSelective(marketAddressExample, MarketAddress.Column.id, MarketAddress.Column.name,
                MarketAddress.Column.userId, MarketAddress.Column.province, MarketAddress.Column.city, MarketAddress.Column.county,
                MarketAddress.Column.addressDetail, MarketAddress.Column.areaCode, MarketAddress.Column.tel, MarketAddress.Column.isDefault, MarketAddress.Column.addTime,
                MarketAddress.Column.updateTime, MarketAddress.Column.deleted);
        return marketAddressList;
    }

}
